<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form class="-mb-15px" size="large" :model="queryParams" ref="queryFormRef" :inline="true" label-width="68px">
      <el-form-item label="维护项" prop="itemId">
        <el-select @change="handleQuery" clearable filterable class="!w-240px" v-model="queryParams.itemId"
          placeholder="请选择">
          <el-option v-for="dict in settingTypeList" :key="dict.id" :label="dict.name" :value="dict.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="设备名称" prop="equipmentId">
        <el-input v-model="queryParams.equipmentName" placeholder="请输入设备名称" clearable @keyup.enter="handleQuery"
          class="!w-240px" />
      </el-form-item>
      <el-form-item label="维护状态" prop="status">
        <el-select @change="handleQuery" v-model="queryParams.status" placeholder="请选择维护状态" clearable class="!w-240px">
          <el-option clearable v-for="dict in getIntDictOptions(DICT_TYPE.KEEP_TYPE)" :key="dict.value"
            :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="责任人" prop="dutyUserName">
        <el-input v-model="queryParams.dutyUserName" placeholder="请输入责任人名称" clearable @keyup.enter="handleQuery"
          class="!w-240px" />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery">
          <Icon icon="ep:search" class="mr-5px" /> 搜索
        </el-button>
        <el-button @click="resetQuery">
          <Icon icon="ep:refresh" class="mr-5px" /> 重置
        </el-button>
        <el-button type="primary" plain @click="setWarning()">
          <Icon icon="ep:plus" class="mr-5px" /> 预警设置
        </el-button>
        <el-button type="success" plain @click="handleExport" :loading="exportLoading">
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table border row-key="id" v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true"
      @selection-change="handleRowCheckboxChange">
      <el-table-column type="selection" width="55" />
      <el-table-column label="设备名称" width="120" align="center" prop="equipmentName" />
      <el-table-column label="设备编码" width="120" align="center" prop="equipmentCode" />
      <el-table-column label="所属部门" width="120" align="center" prop="deptName" />
      <el-table-column label="维护单号" width="180" align="center" prop="keepNo">
        <template #default="{ row }">
          <el-button type="success" text style="font-size: 14px;" @click="openFormView('update', row.id)">
            {{ row.keepNo }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="维护项" width="150" align="center" prop="itemName" />
      <el-table-column label="维护状态" width="100" align="center" prop="status">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.KEEP_TYPE" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="维护周期(天)" width="120" align="center" prop="cycle" />
      <el-table-column label="计划维护日期" align="center" width="150" prop="planDate" />
      <el-table-column label="剩余天数" width="100" align="center" prop="remaining" />
      <el-table-column label="责任人" width="100" align="center" prop="dutyUserName" />
      <el-table-column label="创建时间" align="center" prop="createTime" :formatter="dateFormatter" width="180px" />
      <el-table-column label="操作" fixed="right" align="center" min-width="120px">
        <template #default="scope">
          <el-button link type="primary" v-if="scope.row.status == 10" @click="accept(scope.row.id, 15, '接收')">
            接收
          </el-button>
          <!-- 5待派工  10未接收 15已接收 20维护中 23待审核 25已完成 30已撤销 35 待审核 -->
          <el-button link type="primary" v-if="scope.row.status == 15" @click="accept(scope.row.id, 20, '开始')">
            开始
          </el-button>
          <el-button link type="primary" v-if="scope.row.status == 20" @click="openForm('update', scope.row.id)">
            报工
          </el-button>
          <el-button link type="primary" v-if="scope.row.status == 23 && scope.row.reviewerPermission == true"
            @click="openForm('reviewer', scope.row.id)">
            审核
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination :total="total" v-model:page="queryParams.pageNo" v-model:limit="queryParams.pageSize"
      @pagination="getList" />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <keepTaskForm ref="formRef" @success="getList" />

  <!-- 表单弹窗：添加/修改 -->
  <KeepTaskFormView ref="formViewRef" @success="getList" />

</template>

<script setup lang="ts">
import { isEmpty } from '@/utils/is'
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { EquipmnetKeepWaitApi, EquipmnetKeepWait } from '@/api/business/equipmnetkeepwait'
import keepTaskForm from './KeepTaskForm.vue'
import { getIntDictOptions, DICT_TYPE } from '@/utils/dict'
import { EquipmentKeepSettingTypeApi, EquipmentKeepSettingType } from '@/api/business/equipmentkeepsettingtype'
import { KeepTaskApi, KeepTask } from '@/api/business/keeptask'
import KeepTaskFormView from './KeepTaskFormView.vue'

/** 待维护设备 列表 */
defineOptions({ name: 'EquipmnetKeepWait' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<EquipmnetKeepWait[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  keepNo: undefined,
  itemId: undefined,
  equipmentId: undefined,
  status: undefined,
  dutyUserName: undefined,
  equipmentName: undefined,
  excludeStatus: 5,//排除掉已派工的
})

const queryParamsSettingType = reactive({
  pageNo: 1,
  pageSize: 100,
  keepNo: undefined,
  itemId: undefined,
  equipmentId: undefined,
  status: undefined,
  dutyUserName: undefined,

})

const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

const formViewRef = ref()
const openFormView = (type: string, id?: number) => {
  formViewRef.value.open(type, id)
}

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await EquipmnetKeepWaitApi.getEquipmnetKeepWaitPageDamage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}


/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 确认接收按钮 */
const accept = async (id: number, status: number, title: string) => {
  try {
    // 二次确认
    await message.confirm("确认" + title + "?")
    // 确认接收
    const param = {
      id: id,//待维护
      status: status
    }
    await EquipmnetKeepWaitApi.updateKeepWaitAndTaskDamage(param)
    message.success(title + "成功!")
    // 刷新列表
    await getList()
  } catch { }
}

const dialogVisible = ref(false) // 弹窗的是否展示

//预警设置
const setWarning = () => {
  dialogVisible.value = true
}


/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await EquipmnetKeepWaitApi.deleteEquipmnetKeepWaitDamage(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch { }
}

/** 批量删除待维护设备 */
const handleDeleteBatch = async () => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    await EquipmnetKeepWaitApi.deleteEquipmnetKeepWaitListDamage(checkedIds.value);
    message.success(t('common.delSuccess'))
    await getList();
  } catch { }
}

const checkedIds = ref<number[]>([])
const handleRowCheckboxChange = (records: EquipmnetKeepWait[]) => {
  checkedIds.value = records.map((item) => item.id);
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await EquipmnetKeepWaitApi.exportEquipmnetKeepWaitDamage(queryParams)
    download.excel(data, '待维护设备.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

//所有维护项
const settingTypeList = ref([])

const getSettingTypeList = async () => {
  const data = await EquipmentKeepSettingTypeApi.getEquipmentKeepSettingTypePageDamage(queryParamsSettingType);
  settingTypeList.value = data.list

}

/** 初始化 **/
onMounted(() => {
  getList()
  getSettingTypeList()
})
</script>
